/*
 * Alloy Analyzer
 * Copyright (c) 2002 Massachusetts Institute of Technology
 *
 * The Alloy Analyzer is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * The Alloy Analyzer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the Alloy Analyzer; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package alloy.ast;

import alloy.util.CreationInfo;
import alloy.util.Dbg;

/** an expression that is also a leaf of the AST */

public abstract class LeafExpr extends TypedExpr implements Leaf {

    /** the id of this */
    private LeafId _id;

    /** For debugging: a record of where in the program this leaf was created */
    private CreationInfo _creationInfo = new CreationInfo();

    public CreationInfo getCreationInfo() { return _creationInfo; }

    // convenience constructors
    public LeafExpr(Location location_) {
        super(location_);
    }

    public LeafExpr(Location location_, Node n1_) {
        super(location_, n1_);
    }


    public LeafExpr(Node n1_) {
        super(Location.UNKNOWN, n1_);
    }



    public LeafExpr(Location location_, Node n1_, Node n2_) {
        super(location_, n1_, n2_);
    }


    public LeafExpr(Node n1_, Node n2_) {
        super(Location.UNKNOWN, n1_, n2_);
    }



    public LeafExpr(Location location_, Node n1_, Node n2_, Node n3_) {
        super(location_, n1_, n2_, n3_);
    }


    public LeafExpr(Node n1_, Node n2_, Node n3_) {
        super(Location.UNKNOWN, n1_, n2_, n3_);
    }

    public LeafId getLeafId() {
        return _id;
    }

    public void setLeafId(LeafId id_) {
        if (getType() == null) {
            throw new RuntimeException();
        }
        Dbg.chk(getType() != null, "setting leaf id of untyped leaf");
        _id = id_;
    }

    final void setDescr() {
    super.setDescr();
    if (_id != null) descr += ", id " + _id.toString();
    }

    /** copied node should get same id */
    public Object copy() {
        Object tempcopy = super.copy();
        LeafExpr leafCopy = (LeafExpr)tempcopy;
        if (_id != null) {
            leafCopy.setLeafId(_id);
        }
        return leafCopy;
    }

    public boolean isSameAs(Node node_) {
    if (!super.isSameAs(node_))
        return false;
    if (_id == null) {
        return false;
    }
    return getLeafId().equals(((Leaf)node_).getLeafId());
    }
}


